<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item>
        <el-button type="primary" plain icon="Plus" @click="open=true">新增</el-button>
        <el-button type="info" plain icon="Sort" @click="toggleExpandAll">展开/折叠</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" v-if="refreshTable" :data="tableData" row-key="id" :default-expand-all="isExpandAll" :tree-props="{ children: 'children'}">
      <el-table-column prop="name" label="名称" min-width="150"></el-table-column>
      <el-table-column prop="menuUri" label="菜单Url" min-width="200"></el-table-column>
      <el-table-column prop="icon" label="图标" align="center">
        <template #default="scope">
          <svg-icon :icon-class="scope.row.menuIcon" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.enableStatus==1">显示</el-tag>
          <el-tag type="danger" v-else>隐藏</el-tag>
        </template>
      </el-table-column>enableStatus
      <el-table-column prop="sort" label="排序"></el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog title="新增/编辑部门" v-model="open" width="600px" append-to-body :before-close="handleClose">
      <el-form ref="deptRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="menuIcon">
          <el-popover placement="bottom-start" :width="540" v-model:visible="showChooseIcon" trigger="click" @show="showSelectIcon">
            <template #reference>
              <el-input v-model="form.menuIcon" placeholder="点击选择图标" @blur="showSelectIcon" readonly>
                <template #prefix>
                  <svg-icon v-if="form.menuIcon" :icon-class="form.menuIcon" class="el-input__icon" style="height: 32px;width: 16px;" />
                  <el-icon v-else style="height: 32px;width: 16px;">
                    <search />
                  </el-icon>
                </template>
              </el-input>
            </template>
            <icon-select ref="iconSelectRef" @selected="selected" />
          </el-popover>
        </el-form-item>
        <el-form-item label="菜单Url" prop="menuUri">
          <el-input v-model="form.menuUri" placeholder="请输入菜单Url" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuUri">
          <el-radio-group v-model="form.enableStatus">
            <el-radio :label="1">展示</el-radio>
            <el-radio :label="2">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="显示排序">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="editLoading">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import useCounterStore from "@/store/counter";
import SvgIcon from "@/components/SvgIcon";
import IconSelect from "@/components/IconSelect";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();

const deptList = ref([]);
let depTree = ref([]);
const open = ref(false);
let loading = ref(false);
const showSearch = ref(true);
const title = ref("");
const deptOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);
let editLoading = ref(false);
const showChooseIcon = ref(false);

let tableData = ref([]);

let queryParams = ref({
  name: "",
});
let form = ref({
  parentId: 0,
  name: null,
  sort: 0,
  menuUri: "",
  menuIcon: "",
  menuType: 1,
  enableStatus: 1,
});
let rules = ref({
  parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
  name: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
});

onMounted(() => {
  getList();
});

/** 展示下拉图标 */
function showSelectIcon() {
  proxy.$refs.iconSelectRef.reset();
  showChooseIcon.value = true;
}
/** 选择图标 */
function selected(name) {
  form.value.menuIcon = name;
  showChooseIcon.value = false;
}
/** 查询部门列表 */
async function getList() {
  try {
    loading.value = true;
    let resp = await proxy.$apis.menuList({});
    resp.forEach((element) => {
      element.children = [];
    });
    let items = proxy.$utils.transformArray(resp, "id", "parentId");
    tableData.value = proxy.$utils.sortItems(items, "asc", "sort");
    console.log("菜单", tableData.value);
  } catch (error) {
  } finally {
    loading.value = false;
  }
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    name: "",
  };
  getList();
}
/** 新增按钮操作 */
function handleAdd(row) {
  form.value.parentId = row.id;
  open.value = true;
}
/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}
/** 修改按钮操作 */
function handleUpdate(row) {
  form.value = proxy.$utils.formatFunction(row, { ...form.value });
  form.value.id = row.id;
  open.value = true;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["deptRef"].validate((valid) => {
    if (valid) save();
  });
}
let handleClose = () => {
  form.value = {
    parentId: 0,
    name: null,
    sort: 0,
    menuUri: "",
    menuIcon: "",
    menuType: 1,
    enableStatus: 1,
  };
  proxy.$refs.deptRef.resetFields();
  getList();
  open.value = false;
};
let save = async () => {
  try {
    editLoading.value = true;
    if (form.value.id) {
      await proxy.$apis.menuUpdate(form.value);
    } else {
      await proxy.$apis.menuAdd(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    editLoading.value = false;
  }
};
/** 删除按钮操作 */
async function handleDelete(row) {
  let resp = await proxy.$utils.confirmTips(
    '是否确认删除名称为"' + row.name + '"的数据项?'
  );
  if (!resp) return;
  await proxy.$apis.menuDel(row.id);
  getList();
  proxy.$modal.msgSuccess("删除成功");
}
</script>
